package com.example.prison.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.prison.entity.LeaveManagement;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author jiang
* @description 针对表【leave_management】的数据库操作Mapper
* @createDate 2023-04-06 15:18:51
* @Entity com.example.military.entity.LeaveManagement
*/
@Mapper
public interface LeaveManagementMapper extends BaseMapper<LeaveManagement> {

//    @Select("select * from leave_management order by id desc")
    @Select("<script>select * from leave_management " +
            " <where> " +
            " <if test=\"destination != null and destination != ''\">and destination like concat('%',#{destination},'%') </if> " +
            " <if test=\"beginTime != null and beginTime != ''\">and begin_date &gt;= #{beginTime} </if> " +
            " <if test=\"endTime != null and endTime != ''\">and end_date &lt;= #{endTime} </if> " +
            " <if test=\"status != null and status != ''\">and status like concat('%',#{status},'%') </if> " +
            " </where> " +
            "order by id desc</script>")
    List<LeaveManagement> getLeaves(@Param("destination")String destination, @Param("status")String status,
                                    @Param("beginTime")String beginTime, @Param("endTime")String endTime);

    @Insert("insert into leave_management(name, begin_date, end_date, reason, comments, type, status, destination, depart)" +
            "values (#{name},#{beginDate},#{endDate},#{reason},#{comments},#{type},#{status},#{destination},#{depart})")
    void addLeave(LeaveManagement leaveManagement);

    @Delete("<script>delete from leave_management where id in <foreach collection=\"ids\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item} </foreach> </script>")
    void deleteLeave(@Param("ids") Integer[] ids);
    @Delete("<script>delete from leave_management where id=#{id}</script>")
    void deleteLeaveById(@Param("id") Integer id);

    @Update("update leave_management set status=#{status}, comments=#{comments} where id=#{id} and status='未审核'")
    void checkLeave(LeaveManagement leave);

//    @Update("update leave_management set begin_date=#{beginDate},end_date=#{endDate}," +
//            "reason=#{reason},comments=#{comments},type=#{type},destination=#{destination},depart=#{depart} " +
//            "where id=#{id} and status='未审核'")
    @Update({"<script> UPDATE leave_management",
            "<set>",
            "<if test='leave.beginDate != null'>begin_date = #{leave.beginDate},</if>",
            "<if test='leave.endDate != null'>end_date = #{leave.endDate},</if>",
            "<if test='leave.reason != null'>reason = #{leave.reason},</if>",
            "<if test='leave.comments != null'>comments = #{leave.comments},</if>",
            "<if test='leave.type != null'>type = #{leave.type},</if>",
            "<if test='leave.destination != null'>destination = #{leave.destination},</if>",
            "<if test='leave.depart != null'>depart = #{leave.depart},</if>",
            "</set>",
            "where id = #{id} and status='未审核'</script>"})
    void updateLeave(LeaveManagement leave);

    @Select("select * from leave_management where id=#{id}")
    LeaveManagement getLeavesById(Integer id);

    /** 打卡测试接口 */
    @Select("select * from leave_management where name=#{name} and status=#{status} order by id desc")
    List<LeaveManagement> getLeavesByNameAndStatus(String name,String status);

//    @Select("select * from leave_management where name=#{userName} and status=#{status}")
//    LeaveManagement getLeavesByNameAndStatusAndLogin(String status,String userName);

    @Update("update leave_management set status=#{status} where id=#{id} and status='同意'")
    void updateStatus(LeaveManagement leave);

    @Update("update leave_management set status=#{status} where id=#{id} and status='未销假'")
    void earlyEndLeave(LeaveManagement leave);

    @Update("update leave_management set status=#{status} where id=#{id} and status='未销假'")
    void endLeave(LeaveManagement leave);

    @Select({"<script>",
            "select * from leave_management ",
            "where status=#{status} ",
            "order by id desc ",
            "</script>"})
    List<LeaveManagement> getLeavesByStatus(String status);

    /** 依据状态获取请假列表  普通用户 */
    @Select("select * from leave_management where status=#{status} and name=#{userName} order by id desc ")
    List<LeaveManagement> getLeavesByStatusAndLogin(@Param("status")String status,@Param("userName")String userName);

    /** 依据状态获取请假列表  审核员 */
    @Select("select * from leave_management where status=#{status} and depart=#{depart} order by id desc ")
    List<LeaveManagement> getLeavesByStatusAndLoginDepart(@Param("status")String status,@Param("depart")String depart);

    @Select({"<script>",
            "select l.* from leave_management l ",
            "where l.begin_date between #{beginDate} and #{endDate} ",
            "order by l.id desc",
            "</script>"})
    List<LeaveManagement> getLeavesByTime(@Param("beginDate")String beginDate, @Param("endDate")String endDate);

    /** 依据时间获取请假列表 普通用户 */
    @Select("select l.* from leave_management l " +
            "where l.begin_date between #{beginDate} and #{endDate} and name=#{userName} order by l.id desc")
    List<LeaveManagement> getLeavesByTimeAndLogin(@Param("beginDate")String beginDate, @Param("endDate")String endDate, @Param("userName")String userName);

    /** 依据时间获取请假列表 审核员 */
    @Select("select l.* from leave_management l " +
            "where l.begin_date between #{beginDate} and #{endDate} and depart=#{depart} order by l.id desc")
    List<LeaveManagement> getLeavesByTimeAndLoginDepart(@Param("beginDate")String beginDate, @Param("endDate")String endDate, @Param("depart")String depart);

    @Select("select * from leave_management where name=#{name} and depart=#{depart} order by id desc")
    List<LeaveManagement> getOneLeaveByNameAndDepart(@Param("name")String name, @Param("depart")String depart);

    /** 获取请假列表  普通用户 */
    @Select("select * from leave_management where name=#{name} order by id desc")
    List<LeaveManagement> getOneListByName(@Param("name")String name);

    /** 获取请假列表  审核员 */
    @Select("select * from leave_management where depart=#{depart} order by id desc")
    List<LeaveManagement> getOneListByLoginDepart(@Param("depart")String depart);

    @Select("select * from leave_management where end_date < #{date} and status='未销假' order by id desc")
    List<LeaveManagement> getNoReturn(@Param("date") String date);

    @Select("select * from leave_management where begin_date < #{date} and status='未销假' order by id desc")
    List<LeaveManagement> getLongTime(@Param("date") String date);

    /** 依据请假去向获取请假列表 */
    @Select("select * from leave_management where destination like concat('%',#{destination},'%') order by id desc")
    List<LeaveManagement> getLeaveByDestination(@Param("destination")String destination);

    @Select("select * from leave_management where destination like concat('%',#{destination},'%') and status='未销假' order by id desc")
    List<LeaveManagement> getLeavingByDestination(@Param("destination")String destination);

    @Select("select * from leave_management where status='未销假' order by id desc")
    List<LeaveManagement> getLeaving();

}




